package com.example.star.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.Glide;
import com.example.star.R;
import com.example.star.entity.Post;
import com.example.star.utils.ImageZoomDialogFragment;

import java.util.List;

public class PostAdapter extends ArrayAdapter<Post> {
    private Context context;
    private String userName; // 当前登录用户的用户名
    private Fragment fragment;

    public PostAdapter(Context context, int resource, List<Post> objects, String userName, Fragment fragment) {
        super(context, resource, objects);
        this.context = context;
        this.userName = userName;
        this.fragment = fragment;
    }

    @RequiresApi(api = android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_post, parent, false);
        }

        Post post = getItem(position);

        TextView postTitle = convertView.findViewById(R.id.post_title);
        TextView postContent = convertView.findViewById(R.id.post_content);
        TextView userNamePost = convertView.findViewById(R.id.userName_post);
        ImageView userImage = convertView.findViewById(R.id.user_image);
        ImageView imageMessage = convertView.findViewById(R.id.image_message);

        postTitle.setText(post.getTitle());
        postContent.setText(post.getContent());
        userNamePost.setText(post.getUserName());

        // 判断图片路径是否为空，若为空则隐藏图片视图
        if (post.getImageResource() != null &&!post.getImageResource().isEmpty()) {
            Glide.with(context)
                    .load(post.getImageResource())
                    .placeholder(R.drawable.register)
                    .error(R.drawable.register)
                    .into(imageMessage);
            imageMessage.setVisibility(View.VISIBLE);
            // 设置图片点击监听器
            imageMessage.setOnClickListener(v -> {
                String imageUrl = post.getImageResource();
                ImageZoomDialogFragment dialogFragment = ImageZoomDialogFragment.newInstance(imageUrl);
                FragmentManager fragmentManager = fragment.getChildFragmentManager();
                dialogFragment.show(fragmentManager, "image_zoom_dialog");
            });
        } else {
            imageMessage.setVisibility(View.GONE);
        }

        // 增加空值检查
        if (post.getUserName() != null && post.getUserName().equals(userName)) {
            // 登录用户的帖子在右边
            convertView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        } else {
            // 其他用户的帖子在左边
            convertView.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
        }

        return convertView;
    }
}